/*
 * 支付宝平台(JAVA) SDK
 * 生活号操作包
 */
package org.alipay4j.component.life;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alipay4j.Alipay;
import org.alipay4j.AlipayException;
import org.alipay4j.Configuration;
import org.alipay4j.component.AbstractComponent;
import org.alipay4j.http.AlipayHttpsClient;
import org.alipay4j.http.Response;
import org.alipay4j.model.account.StdPublicBindAccount;
import org.alipay4j.util.ObjectHelper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author 贺亮
 * 生活号账户相关配置组件
 */
public class AccountComponent extends AbstractComponent {

	public AccountComponent(Alipay alipay) {
		super(alipay);
	}
	
	/**
	 * 添加绑定商户会员号
	 * @param bind_account_no 绑定帐号，建议在开发者的系统中保持唯一性
	 * @param display_name 开发者期望在服务窗首页看到的关于该用户的显示信息，最长10个字符
	 * @param agreement_id 账户添加成功，在支付宝与其对应的协议号。如果账户重复添加，接口保证幂等依然视为添加成功，返回此前该账户在支付宝对应的协议号。其他异常该字段不存在。
	 * @param real_name 要绑定的商户会员的真实姓名，最长10个汉字
	 * @param from_user_id 要绑定的商户会员对应的支付宝userid，2088开头长度为16位的字符串
	 * @param remark 备注信息，开发者可以通过该字段纪录其他的额外信息	
	 * @return agreement_id 协议号，商户会员在支付宝服务窗账号中的唯一标识
	 * @throws AlipayException
	 */
	public String accountCreate(String bind_account_no,String display_name,String agreement_id,
			String real_name,String from_user_id,String remark) throws AlipayException {
		if (ObjectHelper.isEmpty(bind_account_no)) {
            throw new IllegalArgumentException("bind_account_no can't be null or empty");
        }
		if (ObjectHelper.isEmpty(display_name)) {
            throw new IllegalArgumentException("display_name can't be null or empty");
        }
		if (ObjectHelper.isEmpty(from_user_id)) {
            throw new IllegalArgumentException("from_user_id can't be null or empty");
        }
		//组装请求参数
		Map<String, String> body = new HashMap<String, String>();
		body.put("bind_account_no", bind_account_no);
		body.put("display_name", display_name);
		body.put("agreement_id", agreement_id);
		body.put("real_name", real_name);
		body.put("from_user_id", from_user_id);
		body.put("remark", remark);
		body.put("method", "alipay.open.public.account.create");
		Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
		//根据请求结果判定，是否验证成功
		JSONObject bodyRes = getObjRes(response, "alipay_open_public_account_create_response");
    	if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_account_create_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
        	String sub_code = bodyRes.getString("sub_code");
        	String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
		return bodyRes.getString("agreement_id");
	}
	
	/**
	 * /**
	 * 重新设置绑定商家会员号
	 * @param bind_account_no 绑定帐号，建议在开发者的系统中保持唯一性
	 * @param display_name 开发者期望在服务窗首页看到的关于该用户的显示信息，最长10个字符
	 * @param agreement_id 需要重置的协议号，商户会员在支付宝服务窗账号中的唯一标识
	 * @param real_name 	要绑定的商户会员的真实姓名，最长10个汉字
	 * @param from_user_id 	要绑定的商户会员对应的支付宝userid，2088开头长度为16位的字符串
	 * @param remark 备注信息，开发者可以通过该字段纪录其他的额外信息
	 * @return agreement_id:重置后的协议号，商户会员在支付宝服务窗账号中的唯一标识
	 * @throws AlipayException
	 */
	public String accountReset(String bind_account_no,String display_name,String agreement_id,
			String real_name,String from_user_id,String remark) throws AlipayException {
		if (ObjectHelper.isEmpty(bind_account_no)) {
            throw new IllegalArgumentException("bind_account_no can't be null or empty");
        }
		if (ObjectHelper.isEmpty(display_name)) {
            throw new IllegalArgumentException("display_name can't be null or empty");
        }
		if (ObjectHelper.isEmpty(agreement_id)) {
            throw new IllegalArgumentException("agreement_id can't be null or empty");
        }
		if (ObjectHelper.isEmpty(from_user_id)) {
            throw new IllegalArgumentException("from_user_id can't be null or empty");
        }
		//组装请求参数
		Map<String, String> body = new HashMap<String, String>();
		body.put("bind_account_no", bind_account_no);
		body.put("display_name", display_name);
		body.put("agreement_id", agreement_id);
		body.put("real_name", real_name);
		body.put("from_user_id", from_user_id);
		body.put("remark", remark);
		body.put("method", "alipay.open.public.account.reset");
		Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
		//根据请求结果判定，是否验证成功
		JSONObject bodyRes = getObjRes(response, "alipay_open_public_account_reset_response");
    	if (Configuration.isDebug()) {
            System.out.println("alipayOpenPublicAccountReset_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
        	String sub_code = bodyRes.getString("sub_code");
        	String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
		return bodyRes.getString("agreement_id");
	}
	
	/**
	 * 查询绑定商户会员号
	 * @param user_id 支付宝账号userid，2088开头长度为16位的字符串
	 * @return List<StdPublicBindAccount> 绑定账户列表
	 * @throws AlipayException
	 */
	public List<StdPublicBindAccount> accountQuery(String user_id) throws AlipayException {
		if (ObjectHelper.isEmpty(user_id)) {
            throw new IllegalArgumentException("user_id can't be null or empty");
        }
		//组装请求参数
		Map<String, String> body = new HashMap<String, String>();
		body.put("user_id", user_id);
		body.put("method", "alipay.open.public.account.query");
		Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
		//根据请求结果判定，是否验证成功
		JSONObject bodyRes = getObjRes(response, "alipay_open_public_account_query_response");
    	if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_account_query_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
        	String sub_code = bodyRes.getString("sub_code");
        	String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        //转换为List
        List<StdPublicBindAccount> accounts = new ArrayList<StdPublicBindAccount>();
        JSONArray array = bodyRes.getJSONArray("public_bind_accounts");
        for(int i=0;i<array.size();i++) {
        	StdPublicBindAccount account = JSONObject.toJavaObject(array.getJSONObject(i), StdPublicBindAccount.class);
        	accounts.add(account);
        }
		return accounts;
	}
	
	/**
	 * 解除绑定商户会员号
	 * @param agreement_id 协议号，商户会员在支付宝服务窗账号中的唯一标识，与bind_account_no不能同时为空
	 * @param bind_account_no 绑定帐号，建议在开发者的系统中保持唯一性，与agreement_id不能同时为空
	 * @return boolean
	 * @throws AlipayException
	 */
	public boolean accountDel(String agreement_id,String bind_account_no) throws AlipayException {
		if (ObjectHelper.isEmpty(agreement_id)&&ObjectHelper.isEmpty(bind_account_no)) {
            throw new IllegalArgumentException("agreement_id && bind_account_no can't be null or empty");
        }
		//组装请求参数
		Map<String, String> body = new HashMap<String, String>();
		body.put("agreement_id", agreement_id);
		body.put("bind_account_no", bind_account_no);
		body.put("method", "	alipay.open.public.account.delete");
		Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
		//根据请求结果判定，是否验证成功
		JSONObject bodyRes = getObjRes(response, "alipay_open_public_account_delete_response");
    	if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_account_delete_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
        	String sub_code = bodyRes.getString("sub_code");
        	String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
		return true;
	}

}
